{extend name="../../base/view/common/base" /}
{block name="style"}
<style>
.layui-table-min th{font-size:13px; text-align:center; background-color:#f8f8f8;}
.layui-table-min td{font-size:13px; padding:6px;text-align:center;}
.layui-form-radio{margin-right:6px}
.layui-form-radio>i{margin-right:3px; }
</style>
{/block}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4">
	<h3 class="pb-3">申请开票</h3>
	<table class="layui-table">
		<tr>
		
			<td class="layui-td-gray">开票类型<font>*</font></td>
			<td>
			<select name="invoice_type" lay-verify="required" lay-reqText="请选择开票类型">
				<option value="">请选择开票类型</option>
				{foreach ['1'=>'增值税专用发票','2'=>'普通发票','3'=>'专业发票',] as $k=>$v}
				<option value="{$k}"{if isset($detail.invoice_type) && $detail.invoice_type==$k} selected{/if} >{$v}</option>
				{/foreach}

			  </select>
			</td>
			<td class="layui-td-gray">开票主体<font>*</font></td>
			<td>
				<select name="invoice_subject" lay-verify="required" lay-reqText="请选择开票主体">
					<option value="">请选择开票主体</option>
					{volist name=":get_department()" id="vo"}
					<option value="{$vo.id}" {if isset($detail.invoice_subject) && $detail.invoice_subject==$vo.id} selected{/if}>{$vo.title}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray">抬头类型<font>*</font></td>
			<td>
				{if!$id}
				<input type="radio" name="type" lay-filter="type" value="1" title="企业" checked>
				<input type="radio" name="type" lay-filter="type" value="2" title="个人">
				{else}
				{foreach ['1'=>'企业','2'=>'个人'] as$tk=> $t}
				<input type="radio" name="type" {if $detail.type==$tk} checked{/if} lay-filter="type" value="{$tk}" title="{$t}">
				{/foreach}
				{/if}
			</td>
		</tr>
		<tr>
			<td class="layui-td-gray">客户名称<font>*</font></td>
			<td>
				<select name="customer_id" id="customer_id" lay-search lay-filter="customer_id" {if isset($detail.customer_id)} disabled{/if}>
					<option value="">选择客户</option>
					{volist name="punterList" id="pl"}
					<option value="{$pl.id}" {if isset($detail.customer_id) && $detail.customer_id==$pl.id} selected{/if}>{$pl.name}</option>
					{/volist}
				</select>
			</td>
			<td class="layui-td-gray">打印模板<font>*</font></td>
			<td>
				<select name="printTemplate" id="printTemplate" lay-search lay-filter="customer_id" >
				{volist name="printTemplateList" id="pl"}
				<option value="{$pl.id}" {if isset($detail.printTemplate) && $detail.printTemplate==$pl.id} selected{/if}>{$pl.name}</option>
				{/volist}
				</select>
			</td>
		</tr>
<!--		<tr>-->
<!--			-->
<!--			<td class="layui-td-gray">开票抬头<font>*</font></td>-->
<!--			<td><input type="text" name="invoice_title" class="layui-input" value="" lay-verify="required"  lay-reqText="请输入开票抬头"></td>-->
<!--			<td class="layui-td-gray">电话号码</td>-->
<!--			<td><input type="text" name="invoice_phone" class="layui-input" value=""></td>-->
<!--		</tr>-->
<!--		<tr class="invoice-type">-->
<!--			<td class="layui-td-gray-2">纳税人识别号</td>-->
<!--			<td><input type="text" name="invoice_tax" class="layui-input" value=""></td>-->
<!--			<td class="layui-td-gray">开户行</td>-->
<!--			<td><input type="text" name="invoice_bank" class="layui-input" value=""></td>-->
<!--			<td class="layui-td-gray">银行账号</td>-->
<!--			<td><input type="text" name="invoice_account" class="layui-input" value=""></td>-->
<!--		</tr>-->
<!--		<tr class="invoice-type">-->
<!--			<td class="layui-td-gray-2">银行营业网点</td>-->
<!--			<td><input type="text" name="invoice_banking" class="layui-input" value=""></td>-->
<!--			<td class="layui-td-gray">地址</td>-->
<!--			<td colspan="3"><input type="text" name="invoice_address" class="layui-input" value=""></td>-->
<!--		</tr>-->
		<tr>{if !isset($detail.status)||$detail.status==1}
			<td colspan="6"><a href="javascript:" class="layui-btn layui-btn-primary " onclick="getMx()">关联销售合同</a></td>
		{/if}
		</tr>
		<tr>
			<td colspan="6">
				<table class="layui-table">
					<thead>
					<tr>
						<td>序号</td>
						<td>品牌</td>
						<td>品名</td>
						<td>型号</td>
						<td>订货号</td>
						<td>计量单位</td>
						<td>合同数量</td>
						<td>合同金额</td>
						<td>开票金额</td>
						<td>关联销售合同</td>
						<td>操作</td>
					</tr>
					</thead>
					<tbody id="mxTbdy">
					{volist name="mxList" id="ml" key="mk"}
					<tr>
						<td>
							<span class="xuhao">{$mk}</span>
							<input type="hidden" name="mx[mxid][]" value="{$ml.id}">
							<input type="hidden" class="mx_mdId" name="mx[mdId][]" value="{$ml.mdId}">
							<input type="hidden" class="sealContractMxId" name="mx[sealContractMxId][]" value="{$ml.sealContractMxId}">
							<input type="hidden" class="sealcontractId" name="mx[sealcontractId][]" value="{$ml.sealcontractId}">
						</td>
						<td>{$ml.brand}</td>
						<td>{$ml.mdName}</td>
						<td>{$ml.specs}</td>
						<td>{$ml.buyCode}</td>
						<td>{$ml.unit}</td>
						<td>{$ml.bjNum}</td>
						<td>{$ml.bjMoney}</td>
						<td><input type="text" name="mx[amount][]" value="{$ml.amount}" class="layui-input " lay-verify="required" oninput="digitalFiltering(this,2)"></td>
						<td>{$ml.xsHt}</td>
						<td>
							{if !isset($detail.status)||$detail.status==1}
							<a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"
							onclick="delMx('{$ml.id}',this)">删除</a>
							{/if}
						</td>
					</tr>
					{/volist}
					</tbody>
				</table>
			</td>
		</tr>
	</table>
	<div class="py-3">
		<input type="hidden" name="id" value="{$id|default=0}">
		{if !isset($detail.status)||$detail.status==1}
		<button class="layui-btn layui-btn-normal " lay-submit="" lay-filter="webform">保存</button>
<!--		<button class="layui-btn layui-btn-normal " lay-submit="" lay-filter="webform">保存并提交审核</button>-->
		<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		{/if}
		{if isset($detail.status)&&$detail.status==1}
		<a href="javascript:" class="layui-btn " onclick="onSure()">确认</a>
		{/if}
	</div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
const moduleInit = ['tool','employeepicker','oaTool'];
function gouguInit() {
	var form = layui.form,upload = layui.upload,tool=layui.tool,table=layui.table,oaTool = layui.oaTool,laydate = layui.laydate;

	//选择抬头类型
	form.on('radio(type)', function (data) {
		if(data.value==2){
			$('.invoice-type').hide();
		}
		else{
			$('.invoice-type').show();
		}
	});

	//相关附件上传
	oaTool.addFile();

	//监听提交
	form.on('submit(webform)', function(data){
		$.ajax({
			url: "/finance/invoice/add",
			type:'post',
			data:data.field,
			success:function(e){
				layer.msg(e.msg);
				if (e.code == 0) {
					tool.sideClose(1000);
				}
			}
		})
		return false;
	});

	form.on('select(flowtype)', function(data){
		var check_type = data.elem[data.elem.selectedIndex].title;
		var formHtml='<td class="layui-td-gray">审核人<font>*</font></td>\
		<td colspan="5">\
			<input type="text" name="check_admin_name" value="" autocomplete="off" placeholder="请选择审核人" lay-verify="required" lay-reqText="请选择审核人" class="layui-input picker-one"><input type="hidden" name="check_admin_ids" value="">\
		</td>';
		if(check_type == 2){
			$('#flow_tr').html(formHtml);
			form.render();
		}
		if(data.value==''){
			return false;
		}
		$.ajax({
			url: "/api/index/get_flow_users",
			type:'get',
			data:{id:data.value},
			success: function (e) {
				if (e.code == 0) {
					var flowLi='';
					var flow_data = e.data.flow_data;
					if(e.data.copy_uids && e.data.copy_uids !=''){
						$('[name="copy_names"]').val(e.data.copy_unames);
						$('[name="copy_uids"]').val(e.data.copy_uids.split(','));
					}
					if(check_type == 1 || check_type == 3){
						for(var a=0;a<flow_data.length;a++){
							var userList='',sign_type = '';
							if(check_type == 1){
								if(flow_data[a].flow_type==1){
									userList+= '<li style="padding:3px 0">当前部门负责人</li>';
								}
								else if(flow_data[a].flow_type==2){
									userList+= '<li style="padding:3px 0">上级部门负责人</li>';
								}
								else{
									if(flow_data[a].flow_type==3){
										sign_type= ' <span class="layui-badge layui-bg-blue">或签</span>';
									}
									if(flow_data[a].flow_type==4){
										sign_type= ' <span class="layui-badge layui-bg-blue">会签</span>';
									}
									for(var b=0;b<flow_data[a].user_id_info.length;b++){
										userList+= '<li style="padding:3px 0"><img src="'+flow_data[a].user_id_info[b].thumb+'" style="width:24px; height:24px; border-radius:50%; margin-right:8px;" />'+flow_data[a].user_id_info[b].name+'</li>';
									}
								}
							}
							else if(check_type == 3){
								sign_type= ' <span class="layui-badge layui-bg-blue">'+flow_data[a].flow_name+'</span>'
								for(var b=0;b<flow_data[a].user_id_info.length;b++){
									userList+= '<li style="padding:3px 0"><img src="'+flow_data[a].user_id_info[b].thumb+'" style="width:24px; height:24px; border-radius:50%; margin-right:8px;" />'+flow_data[a].user_id_info[b].name+'</li>';
								}
							}
							flowLi+='<li class="layui-timeline-item">\
								<i class="layui-icon layui-timeline-axis">&#xe63f;</i>\
								<div class="layui-timeline-content">\
								  <p class="layui-timeline-title"><strong>第'+(a+1)+'级审批</strong>'+sign_type+'</p>\
								  <ul>'+userList+'</ul>\
								</div>\
							</li>';
						}
						formHtml = '<td class="layui-td-gray">审批流程</td>\
									<td colspan="7">\
										<ul id="flowList" class="layui-timeline">'+flowLi+'</ul>\
									</td>';
						$('#flow_tr').html(formHtml);
					}
				}
			}
		})
	});

}


function getMx() {
	let punterId=$("#customer_id").val();
	if (punterId==''){
		layer.msg('请选择客户');
		return;
	}
	let inputs=document.querySelectorAll('.sealContractMxId');
	let sealContractMxIds=[];
	inputs.forEach(function (input) {
		sealContractMxIds.push(input.value);
	})

	boxModel("{:url('addMx')}?punterId="+punterId+'&sealContractMxIds='+sealContractMxIds.join(','),'关联销售合同','1000')

}

let mxIndex = '';

function saveAddMx(ojb) {
	var lastListItem = $('.tableMany tr:last');
	let aa = $(lastListItem).find(".xuhao")
	layer.close(mxIndex)
	console.log('aaa', ojb)
	let len = $(aa).text()
	if (!parseInt(len)) {
		len = 0;
	}

	let index = parseInt(len) + 1;
	let str = '';
	for (var i = 0; i < ojb.length; i++) {
		let item = ojb[i];
		str += '    <tr>\n' +
				'                        <td>\n' +
				'                            <span class="xuhao">'+index+'</span>\n' +
				'                            <input type="hidden" name="mx[mxid][]" value="">\n' +
				'                            <input type="hidden" class="mx_mdId" name="mx[mdId][]" value="'+item.mdId+'">\n' +
				'                            <input type="hidden" class="sealContractMxId" name="mx[sealContractMxId][]" value="'+item.id+'">\n' +
				'                            <input type="hidden" class="sealcontractId" name="mx[sealcontractId][]" value="'+item.orderId+'">\n' +
				'                        </td>\n' +
				'                        <td>'+item.brand+'</td>\n' +
				'                        <td>'+item.mdName+'</td>\n' +
				'                        <td>'+item.specs+'</td>\n' +
				'                        <td>'+item.buyCode+'</td>\n' +
				'                        <td>'+item.unit+'</td>\n' +
				'                        <td>'+item.bjNum+'</td>\n' +
				'                        <td>'+item.bjMoney+'</td>\n' +
				'                       <td><input type="text" name="mx[amount][]" value="'+item.bjMoney+'" class="layui-input " lay-verify="required" oninput="digitalFiltering(this,2)"></td>\n' +
				'                        <td>'+item.xsHt+'</td>\n' +
				'                        <td>\n' +
				'                            {if !isset($detail.status)||$detail.status==1}\n' +
				'                            <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"\n' +
				'                               onclick="delMx(\'\',this)">删除</a>\n' +
				'                            {/if}\n' +
				'                        </td>\n' +
				'                    </tr>';
		index++;
	}
	$("#mxTbdy").append(str)
	layui.form.render();
}

function delMx(id = '', e) {
	if (id == '') {
		$(e).parent().parent().remove()
		return;
	}
	$.ajax({
		url: "{:url('removeMx')}",
		type: 'post',
		dataType: 'json',
		data: {id: id},
		success: function (res) {
			if (res.code != 0) {
				layer.msg(res.msg)
			} else {
				$(e).parent().parent().remove()
			}
		},
		error(e) {
			layer.msg('网络错误，请稍后')
		}
	})
}

function onSure() {
	httpRequest("{:url('onSure')}",{id:"{$id}"},function (res) {
		if (res.code==0){
			parent.parent.location.reload();
		}
	})
}
</script>
{/block}
<!-- /脚本 -->